#coding:utf-8
#! ‪C:\Developer\python36\python3.exe
import urllib
import selenium
import time

from PIL import Image
from selenium.webdriver.remote.command import Command
import selenium.webdriver
from selenium.webdriver.common.action_chains import ActionChains
import lxml
import lxml.etree
import go
from getMore import *

def crackVerify(driver,url):#driver用来操作浏览器，url用来命名
    driver.save_screenshot('windowShot.png')#将整个屏幕截图
    element = driver.find_element_by_xpath('//img[@class="Captcha-image"]')#找到验证码的标签
    #得到验证码对于窗口的尺寸
    left = element.location['x']
    top = element.location['y']
    right = element.location['x'] + element.size['width']
    bottom = element.location['y'] + element.size['height']
    #根据验证码的尺寸将其从整个屏幕的截图中截取下来
    im = Image.open('windowShot.png')
    im = im.crop((left, top, right, bottom))
    filePath = './imgs/' + url[35:-23] + '.gif'
    im.save(filePath)
    time.sleep(1)
    pos = []
    while len(pos)==0:
        pos = go.getPos(filePath)#调用tensorflow识别截取到的验证码
    # [[20.90747051446678, 25.881781355268096], [98.792935205130135, 26.083687708512485]]  200  44
    for i in range(len(pos)):#遍历识别后返回的结果集
        word = pos[i]#取出第i个字的坐标数据
        x = word[0]
        x = int(x)#得到第i个字的X值
        imgNode = driver.find_element_by_class_name('Captcha-image')#找到验证码标签
        ActionChains(driver).move_to_element(imgNode).perform()#讲鼠标移到验证码的标签上去，此时指针在验证码的正中间
        X = x - 100#计算出指针需要移动的距离，负数向左移，正数向右移
        print(X)
        driver.execute(Command.MOVE_TO, {'xoffset': X, 'yoffset': 0})#移动指针到指定位置并点击
        driver.execute(Command.CLICK)
        time.sleep(2)
        #将光标放回密码输入框
        pwd = driver.find_element_by_name('password')
        ActionChains(driver).move_to_element(pwd).perform()
        driver.execute(Command.CLICK)
    sign = driver.find_element_by_class_name('sign-button')#点击登录按钮，若验证码识别正确则成功登录，若错误则加载新的验证码
    sign.click()





#
# myFile = open('html.txt',encoding='utf-8')
# myHtml = myFile.read()
# myTree = lxml.etree.HTML(myHtml)
# divList = myTree.xpath('//div[@class="TopstoryMain"]/div//div')
# print(len(divList))
# file = open('zhihu.txt','w',encoding='utf-8')
# time.sleep(1)
# for div in divList:
#     title = div.xpath('./div[1]/div[2]/h2/div/a/text()')
#     if len(title):
#         file.write(title[0]+'\n')
#         print(title)
#     else:
#         break
#     man = div.xpath('./div[1]/div[2]/div/div/div/div/div/span/div/div/a/text()')
#     if len(man):
#         file.write(man[0]+'\n')
#     else:break
#     answerList =div.xpath('./div[1]/div[2]/div[2]/div[2]/span//p')
#     if len(answerList)==0:
#         break
#     for p in answerList:
#         answer = p.xpath('./text()')
#         if len(answer):
#             file.write(answer[0]+'\n')
#     times = div.xpath('./div[1]/div[2]/div[2]/div[3]/a/span/text()')
#     if len(times):
#         file.write(times[0])
#     file.write('\n\n\n\n\n')
#
# file.close()
#

